﻿using gbL.NetBoard.Examples.DoubleMVC.View.UI.Requests;
using gbL.NetBoard.Helpers;

namespace gbL.NetBoard.Examples.DoubleMVC.View.UI
{
    class DetailView : TypedMessageListener<DetailRequest>
    {
        public IOutputTarget Output { get; set; }

        public DetailView(Blackboard domain) : this(domain, new ConsoleOutputTarget()) { }

        public DetailView(Blackboard domain, IOutputTarget target) : base(domain) { Output = target; }

        protected override void OnMessageAdded(DetailRequest message)
        {
            var taken = Messages.Take(e => e.Message == message);
            if (taken.Count == 0) return;

            Output.WriteLine();
            Output.WriteLine(message.Title);
            Output.WriteLine(new string('-', message.Title.Length));

            ColumnDef<string> headings = new ColumnDef<string>("Headings", x => x, false);
            headings.CalculateWidth(message.Values.Keys);
            
            foreach (string key in message.Values.Keys)
            {
                headings.OutputColumnCell(key, Output, false);
                Output.WriteLine("   {0}", message.Values[key]);
            }
        }

        protected override void OnMessageRemoved(DetailRequest message)
        {
            //
        }
    }
}
